package cn.stylefeng.guns.modular.system.service;

import cn.stylefeng.guns.core.common.constant.state.ManagerStatus;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.system.entity.SchoolCode;
import cn.stylefeng.guns.modular.system.entity.SysSchool;
import cn.stylefeng.guns.modular.system.entity.SysStudent;
import cn.stylefeng.guns.modular.system.entity.User;
import cn.stylefeng.guns.modular.system.factory.UserFactory;
import cn.stylefeng.guns.modular.system.mapper.SysSchoolMapper;
import cn.stylefeng.guns.modular.system.model.SchoolDto;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 学校用户关联表 服务类
 * </p>
 *
 * @author zfy
 * @since 2020-08-06
 */
@Service
public class SysSchoolService extends ServiceImpl<SysSchoolMapper,SysSchool> {
    @Autowired
    private UserService userService;
    @Autowired
    private SysSchoolAgentService schoolAgentService;

    public IPage<SchoolDto> selectUsers(String name, Integer examine,Long createUser) {
//        String createUsers = "";
        String[] createUsers = null;
        if(ShiroKit.hasRole("agent")){
            createUsers = schoolAgentService.getSchoolByAgent().split(",");
        }
        return this.baseMapper.schoolList(LayuiPageFactory.defaultPage(),name,examine,createUser,createUsers);
    }

    public SysSchool getSysSchool (Long userId){
        SysSchool sysSchool = this.getOne(new QueryWrapper<SysSchool>()
                .eq("user_id", userId));
        return ToolUtil.isEmpty(sysSchool)?new SysSchool():sysSchool;
    }

    public void schoolSave(SchoolDto schoolDto) {
        // 判断账号是否重复
        User theUser = userService.getByAccount(schoolDto.getAccount());
        if (theUser != null
                && (ToolUtil.isEmpty(schoolDto.getUserId()) || !theUser.getUserId().equals(schoolDto.getUserId()))) {
            throw new ServiceException(BizExceptionEnum.USER_ALREADY_REG);
        }
        // 完善账号信息
        String salt = "";
        String password = "";
        if(ToolUtil.isNotEmpty(schoolDto.getPassword())){
            salt = ShiroKit.getRandomSalt(5);
            password = ShiroKit.md5(schoolDto.getPassword(), salt);
        }
        if(ToolUtil.isEmpty(schoolDto.getUserId())){
            schoolDto.setStatus(ManagerStatus.FREEZED.getCode());
        }
        schoolDto.setCreateUser(1L);
        User user = UserFactory.createUser(schoolDto, password, salt);
        user.setPhone(user.getAccount());
        user.setRoleId("1291579798998609922");
        user.setExamine(0);
        userService.saveOrUpdate(user);

        SysSchool sysSchool = getSysSchool (user.getUserId());
        BeanUtils.copyProperties(schoolDto, sysSchool);
        sysSchool.setUserId(user.getUserId());
        sysSchool.setCreateTime(LocalDateTime.now());
        this.saveOrUpdate(sysSchool);
    }
}
